package net.easyjava.base.manager;

import net.easyjava.base.bean.UserBean;
import net.easyjava.base.constants.UserConstants;
import net.easyjava.base.service.UserService;
import net.easyjava.framework.web.security.UserSecurityAdminCookie;
import net.easyjava.framework.web.security.UserSecurityBean;
import net.easyjava.framework.web.security.UserSecurityCookie;
import net.easyjava.tools.http.request.RequestTools;
import net.easyjava.tools.lang.StringTools;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Service
public class UserManager extends BaseManager {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private UserService userService;
    @Resource
    private UserSecurityCookie userSecurityCookie;
    @Resource
    private UserSecurityAdminCookie userSecurityAdminCookie;




    /**
     * 用户进入网站后会默认获取一个默认用户，如果网站已有缓存则取出
     * @return
     */
    public ModelBean userCookie(HttpServletRequest request,HttpServletResponse response){
        UserBean userBean = new UserBean();
        userBean.setStatus(UserConstants.STATUS_REQUEST);
        try {

            String userId = this.getUserId();
            if(!StringTools.isNull(userId)){
//                throw new NullPointerException("that's no user in request");
                return new ModelBean(userService.find(userId));
            }


            userBean = userService.save(userBean);

            //将用户放入到cookie中
            userSecurityCookie.setLongUserSecurityBean(response, userBean.getId(), RequestTools.getIP(request));

            return new ModelBean(userBean);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return new ModelBean(ModelConstants.STATUS_ERROR,ModelConstants.MESSAGE_ERROR);
        }
    }

    /**
     * 后台使用用户名登陆
     * @param request
     * @param response
     * @return
     */
    public ModelBean loginByName(HttpServletRequest request,HttpServletResponse response){
        UserBean userBean = new UserBean();
        userBean.setStatus(UserConstants.STATUS_REQUEST);
        try {

            String userName = RequestTools.getParam(request,"userName");
            String password = RequestTools.getParam(request, "password");


            userBean = userService.find(userName, password);
            if(userBean == null){
                return new ModelBean(ModelConstants.STATUS_ERROR,"用户名或密码错误");
            }

            //将用户放入到cookie中,保存30分钟
            userSecurityAdminCookie.setShortUserSecurityBean(response, userBean.getId(), RequestTools.getIP(request), userSecurityCookie.MAXAGE_30M);

            return new ModelBean(userBean);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return new ModelBean(ModelConstants.STATUS_ERROR,ModelConstants.MESSAGE_ERROR);
        }
    }



}
